var template = require('template/template.js');

App({
  onLaunch: function (options) {
    this.getOpenId();
  },

  onShow(options) {
    console.log('小程序跳传参===', options)
    if (options.referrerInfo.extraData) {
      //根据unid登录
      if (options.referrerInfo.extraData.unid) {
        this.unToLogin(options.referrerInfo.extraData.unid);
        this.data.unionId = options.referrerInfo.extraData.unid
      }
      //根据控价登录
      if (options.referrerInfo.extraData.kjid) this.data.kjid = options.referrerInfo.extraData.kjid
    }
  },

  //获取openId 和 session_key
  getOpenId() {
    var _this = this;
    wx.login({
      success(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            method: 'GET',
            url: _this.data.url + '/API/WeChatApplet.ashx?action=GetOpenId',
            data: {
              js_code: res.code,
              appid: 'wx53e80a0928dcda35',
              secret: 'e6fc84682eadfec28692785dcdb545c0',
              Type: 2
            },
            success(res) {
              _this.data.openid = res.data.openid;
              _this.data.sessionKey = res.data.session_key;
            }
          })
        }
      }
    })
  },

  // 根据openid 登录
  toLogin: function (e,i) {
    wx.request({
      method: 'GET',
      url: this.data.url + '/API/PublicHandler.ashx?action=QuickLogin',
      data: {
        session_key: this.data.sessionKey,
        openId: this.data.openid,
        encryptedData: e,
        iv: i,
        nickName: this.data.userName,
        headImage: this.data.userAvatar,
        shopType: 2, // 2为主流商城，不传则为引流商城
        openType: 'hmeshop.plugins.openid.wxapplet'
      },
      success: res => {
        console.log('根据openid 登录======', res)
        this.data.cookie = res.data.Cookie;
        this.data.unionId = res.data.Data.UnionId;
        this.toReferral()
      }
    })
  },

  // 控价id登录
  kjToLogin(e,i) {
    let _this = this;
    wx.request({
      method: 'GET',
      url: this.data.url + `/API/KjAgentHandler.ashx?action=KjAgentToLogin&kjid=${encodeURIComponent(this.data.kjid)}`,
      data: {
        session_key: this.data.sessionKey,
        openId: this.data.openid,
        kjid: this.data.kjid,
        encryptedData: e,
        iv: i,
        shopType: 2, // 2为主流商城，不传则为引流商城
        openType: 'hmeshop.plugins.openid.wxapplet'
      },
      success(res) {
        console.log('控价id登录===', res)
        if (res.data.Status == 'Success') _this.data.cookie = res.data.Cookie;
        else wx.showToast({ icon: 'none', title: res.data.Message })
      }
    })
  },

  // 根据unionid登陆
  unToLogin(id) {
    let _this = this;
    wx.request({
      method: 'GET',
      url: this.data.url + '/API/WeChatAppletO2O.ashx?action=LoginByUnionId',
      data: { UnionId: id },
      success(res) {
        console.log('根据unionid登陆===', res)
        if (res.data.Status == 'Success') _this.data.cookie = res.data.Cookie;
        else wx.showToast({ icon: 'none', itle: res.data.Message })
      }
    })
  },

  // 绑定上下级关系  899  wx.getStorageSync('rid')
  toReferral() {
    if (wx.getStorageSync('rid')) {
      wx.request({
        method: 'GET',
        url: this.data.url + '/API/QrcodeHandler.ashx?action=BindTemReferral',
        header: { Cookie: this.data.cookie },
        data: { ReferralUserId: wx.getStorageSync('rid') },
        success(res) {
          // wx.showToast({ icon: 'none', title: res.data.Message })
        }
      })
    }
  },

  globalData: {
    template: template
  },

  data: {
    cookie: null, // cookie
    navHeight: null, // 导航栏高度
    statusBarHeight: null, // 状态栏高度
    userInfo: null,  // 用户信息
    userAvatar: '',  //用户头像（getUserInfo获取的）
    userName: '', //用户昵称（getUserInfo获取的）
    openid: null,
    cartNum: 0, //购物车总条数
    sku: null, // 商品规格
    unionId: null, // 请求接口获取的unionId
    kjid: null, //控价id
    sessionKey: null, // 获取session_ke
    // 线上的小图标地址
    imgurl: 'http://img.hmeshop.cn/hmeshop_jxy/images/',
    // 线上
    url: 'https://yytv3.hmeshop.cn'
  }
})